home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / langs / iconv8_l.arc / PROGS.ARC / format.icn < prev    next >
Encoding:
Text File  |  1990-03-08  |  4.1 KB  |  141 lines

  1. ############################################################################
  2. #
  3. #    Name:    format.icn
  4. #
  5. #    Title:    Filter to word wrap a range of text
  6. #
  7. #    Author:    Robert J. Alexander
  8. #
  9. #    Date:    December 5, 1989
  10. #
  11. ############################################################################
  12. #
  13. #  Filter to word wrap a range of text.
  14. #
  15. #  A number of options are available, including full justification (see
  16. #  usage text, below).  All lines that have the same indentation as the
  17. #  first line (or same comment leading character format if -c option)
  18. #  are wrapped.  Other lines are left as is.
  19. #
  20. #  This program is useful in conjunction with editors that can invoke
  21. #  filters on a range of selected text.
  22. #
  23. #  The -c option attemps to establish the form of a comment based on the
  24. #  first line, then does its best to deal properly with the following
  25. #  lines.  The types of comment lines that are handled are those in
  26. #  which each line starts with a "comment" character string (possibly
  27. #  preceded by spaces).  While formatting comment lines, text lines
  28. #  following the prototype line that don't match the prototype but are
  29. #  flush with the left margin are also formatted as comments.  This
  30. #  feature simplifies initially entering lengthy comments or making
  31. #  major modifications, since new text can be entered without concern
  32. #  for comment formatting, which will be done automatically later.
  33. #
  34. ############################################################################
  35. #
  36. #  Links: options
  37. #
  38. ############################################################################
  39.  
  40. link options
  41.  
  42. global width
  43.  
  44. procedure main(arg)
  45.    local usage, opts, tabs, comment, format, just1, space, nspace, wchar
  46.    local line, pre, empty, outline, spaces, word, len
  47.    #
  48.    #  Process the options.
  49.    #
  50.    usage := 
  51.      "usage: ifmt [-n] [-w N] [-t N]\n_
  52.             \t-w N\tspecify line width (default 72)\n_
  53.             \t-t N\tspecify tab width (default 8)\n_
  54.             \t-j\tfully justify lines\n_
  55.             \t-J\tfully justify last line\n_
  56.             \t-c\tattemp to format program comments\n_
  57.             \t-h\tprint help message"
  58.    opts := options(arg,"ht+w+cjJ")
  59.    if \opts["h"] then stop(usage)
  60.    width := \opts["w"] | 72
  61.    tabs := \opts["t"] | 8
  62.    comment := opts["c"]
  63.    format := if \opts["j"] then justify else 1
  64.    just1 := opts["J"]
  65.    #
  66.    #  Initialize variables.
  67.    #
  68.    space := ' \t'
  69.    nspace := ~space
  70.    wchar := nspace
  71.    #
  72.    #  Read the first line to establish a prototype of comment format
  73.    #  if -c option, or of leading spaces if normal formatting.
  74.    #
  75.    line := ((tabs >= 2,detab) | 1)(read(),tabs) | exit()
  76.    line ?
  77.       pre := (tab(many(space)) | "") ||
  78.      if \comment then
  79.         tab(many(nspace)) || tab(many(space)) |
  80.             stop("### Can't establish comment pattern")
  81.      else
  82.         ""
  83.    width -:= *pre
  84.    empty := trim(pre)
  85.    outline := spaces := ""
  86.    repeat {
  87.       line ? {
  88.      #
  89.      #  If this line indicates a formatting break...
  90.      #
  91.      if (=empty & pos(0)) | (=pre & any(space) | pos(0)) |
  92.             (/comment & not match(pre)) then {
  93.         write(pre,"" ~== outline)
  94.         outline := spaces := ""
  95.         write(line)
  96.         }
  97.      #
  98.      #  Otherwise continue formatting.
  99.      #
  100.      else {
  101.         =pre
  102.         tab(0) ? {
  103.            tab(many(space))
  104.            while word := tab(many(wchar)) & (tab(many(space)) | "") do {
  105.           if *outline + *spaces + *word > width then {
  106.              write(pre,"" ~== format(outline))
  107.              outline := spaces := ""
  108.              }
  109.           outline ||:= spaces || word
  110.           spaces := if any('.:?!',word[-1]) then "  " else " "
  111.           }
  112.            }
  113.         }
  114.      }
  115.       line := ((tabs >= 2,detab) | 1)(read(),tabs) | break
  116.       }
  117.    write(((tabs >= 2,entab) | 1)(pre,tabs),
  118.      "" ~== (if \just1 then justify else 1)(outline))
  119. end
  120.  
  121. #
  122. #  justify() -- add spaces between words until the line length = "width".
  123. #
  124. procedure justify(s)
  125.    local min, spaces, len
  126.  
  127.    while *s < width do {
  128.       min := 10000
  129.       s ? {
  130.      while tab(find(" ")) do {
  131.         len := *tab(many(' '))
  132.         if min >:= len then spaces := []
  133.         if len = min then put(spaces,&pos)
  134.         }
  135.      }
  136.       if /spaces then break
  137.       s[?spaces+:0] := " "
  138.       }
  139.    return s
  140. end
  141.